<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Happy dong
 * Created Time: 2019-07-26 15:09
 *
 * Version: 2.0
 */

namespace App\Modules\Employee\Criterias;


use Flower\Core\Repository\Contracts\RepositoryInterface;
use Flower\Core\Repository\Criterias\BaseCriterias;

class GetWxNearbyNetRedCriteria extends BaseCriterias
{
    private $member_id;
    private $adcode;

	public function __construct($member_id,$adcode)
	{
        $this->member_id = $member_id;
        $this->adcode = $adcode;
	}


	public function apply($model, RepositoryInterface $repository)
	{
		$adcode = $this->adcode;
		$adcode =substr($adcode,0,3);
		$res =  $model->selectRaw('hx_user.id,hx_user.nick as barberNick, hx_picture.image as barberPortrait, hx_store.id as storeId')
			->leftJoin('user_store',function($join)use($adcode){
				return $join->on('user_store.uid','user.id')
					->where(['user_store.rid'=>'2','is_work'=>'1']);
			})
			->leftJoin('store',function($joins)use($adcode){
				return $joins->on('user_store.sid','store.id')
					->where('store.adcode','like','%'.$adcode.'%');
			})
			->leftJoin('picture','user.portrait','picture.id')
            ->withCount('userLike')
			->orderBy('user_like_count','desc')
			->whereNotNull('store.id')
            ->groupBy('user.id')
			;
		return $res;
	}
}